package scuff.msg.util

import scuff.Surgeon

/**
 * Outgoing message as [[java.util.Map]] for
 * reflective APIs.
 */
class OutgoingMessage[+PL <: AnyRef](val id: String, val payload: PL) extends java.util.Map[String, AnyRef] {

  @transient private val map = {
    val m = new java.util.LinkedHashMap[String, AnyRef]
    m.put("id", id)
    if (hasPayload) m.put("payload", payload)
    m
  }
  
  def hasPayload = payload != null && Surgeon.hasSerializableFields(payload.getClass)
  
  def get(key: Any) = map.get(key)
  def containsKey(key: Any) = map.containsKey(key)
  def containsValue(v: Any) = map.containsValue(v)
  def size = map.size
  def put(key: String, value: AnyRef) = throw new UnsupportedOperationException
  def putAll(map: java.util.Map[_ <: String, _ <: AnyRef]) = throw new UnsupportedOperationException
  def remove(key: Any) = throw new UnsupportedOperationException
  def clear = throw new UnsupportedOperationException
  def isEmpty = false
  def entrySet = map.entrySet
  def keySet = map.keySet
  def values = map.values

}
